﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TunnelBackground
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
            ReadStatusAndTypeTable();
        }
        TunnelDCS.DataConnectionServiceClient tDCS = new TunnelDCS.DataConnectionServiceClient();
        private void button1_Click(object sender, EventArgs e)
        {
            //tDCS.ChangeDeviceStatusAsync("Light_SunnyS_150W1-1", "012", "熄灭", "Light_SunnyS_150W1", "2017/09/09 09:09:09");
            //tDCS.ChangeDeviceStatusAsync(textBox1.Text,textBox2.Text,textBox3.Text,textBox4.Text,dateTimePicker1.Text);

            

        }

        

        
        private string getTypeFromDeviceID(string deviceId)
        {
            var dr = from s1 in idAndType.AsEnumerable()
                     where s1.Field<string>("DeviceID") == deviceId
                     select s1;
            DataRow s = dr.ToList()[0];
            string deviceTypeID = s["TypeID"].ToString();
            return deviceTypeID;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string paraname = textBox1.Text;
            string paravalue = textBox2.Text;
            ChangeDeviceStatus(paraname, paravalue);

        }

        #region 根据反馈值修改界面和数据库中的设备状态
        /// <summary>
        /// 本表记录了变量名与设备ID之间的关系
        /// </summary>
        DataTable runStatus = new DataTable();
        /// <summary>
        /// 本表记录了设备ID与类型ID之间的关系
        /// </summary>
        DataTable idAndType = new DataTable();
        /// <summary>
        /// 填充runStatus和idAndType
        /// </summary>
        private void ReadStatusAndTypeTable()
        {
            string sqlRunStatus = "SELECT RSID, ParamStatus, DeviceID, ParamName, RParamName FROM R_RunStatus";
            string sqlIdAndType = "SELECT DeviceID,TypeID FROM M_Device";
            SqlDBHelper sdh = new SqlDBHelper();
            runStatus = sdh.GetTable(sqlRunStatus);
            idAndType = sdh.GetTable(sqlIdAndType);
        }

        /// <summary>
        /// 修改设备状态
        /// </summary>
        /// <param name="paraname">变量名</param>
        /// <param name="paravalue">变量值</param>
        private void ChangeDeviceStatus(string paraname,string paravalue)
        {
            var dr = from s1 in runStatus.AsEnumerable()
                     where s1.Field<string>("RParamName") == paraname
                     select s1;
            DataRow s = dr.ToList()[0];
            string deviceID = s["DeviceID"].ToString();
            string paramStatus = s["ParamStatus"].ToString();
            SqlDBHelper sdh = new SqlDBHelper();
            switch (paramStatus)
            {
                case "ON":
                    {
                        if (paravalue == "True")
                        {
                            tDCS.ChangeDeviceStatus(deviceID, "023", "点亮", deviceID, DateTime.Now.ToString());
                            string sql = "UPDATE M_Device SET DeviceStatusID='023',UpdateTime=GETDATE() WHERE TypeID='" + deviceID + "'";
                            sdh.ExecuteCommand(sql);
                            
                        }
                        if (paravalue == "False")
                        {
                            tDCS.ChangeDeviceStatus(deviceID, "024", "熄灭", getTypeFromDeviceID(deviceID), DateTime.Now.ToString());
                            string sql = "UPDATE M_Device SET DeviceStatusID='024',UpdateTime=GETDATE() WHERE TypeID='" + deviceID + "'";
                            sdh.ExecuteCommand(sql);
                            
                        }
                    };
                    break;
                case "ZZ":
                    {
                        if (paravalue == "True")
                        {
                            tDCS.ChangeDeviceStatus(deviceID, "002", "正转", getTypeFromDeviceID(deviceID), DateTime.Now.ToString());
                            string sql = "UPDATE M_Device SET DeviceStatusID='002',UpdateTime=GETDATE() WHERE DeviceID='" + deviceID + "'";
                            sdh.ExecuteCommand(sql);
                            
                        }
                    };
                    break;
                case "FZ":
                    {
                        if (paravalue == "True")
                        {
                            tDCS.ChangeDeviceStatus(deviceID, "003", "反转", getTypeFromDeviceID(deviceID), DateTime.Now.ToString());
                            string sql = "UPDATE M_Device SET DeviceStatusID='003',UpdateTime=GETDATE() WHERE DeviceID='" + deviceID + "'";
                            sdh.ExecuteCommand(sql);
                            
                        }
                    };
                    break;
                case "TZ":
                    {
                        if (paravalue == "True")
                        {
                            tDCS.ChangeDeviceStatus(deviceID, "001", "停止", getTypeFromDeviceID(deviceID), DateTime.Now.ToString());
                            string sql = "UPDATE M_Device SET DeviceStatusID='001',UpdateTime=GETDATE() WHERE DeviceID='" + deviceID + "'";
                            sdh.ExecuteCommand(sql);
                            
                        }
                    };
                    break;
                case "GZ":
                    {
                        if (paravalue == "True")
                        {
                            tDCS.ChangeDeviceStatus(deviceID, "004", "故障", getTypeFromDeviceID(deviceID), DateTime.Now.ToString());
                            string sql = "UPDATE M_Device SET DeviceStatusID='004',UpdateTime=GETDATE() WHERE DeviceID='" + deviceID + "'";
                            sdh.ExecuteCommand(sql);

                        }
                    };
                    break;
                case "SZ":
                    {
                        if (paravalue == "True")
                        {
                            tDCS.ChangeDeviceStatus(deviceID, "005", "手动", getTypeFromDeviceID(deviceID), DateTime.Now.ToString());
                            string sql = "UPDATE M_Device SET DeviceStatusID='005',UpdateTime=GETDATE() WHERE DeviceID='" + deviceID + "'";
                            sdh.ExecuteCommand(sql);

                        }
                    };
                    break;
                case "GREEN":
                    {
                        if (paravalue == "True")
                        {
                            tDCS.ChangeDeviceStatus(deviceID, "053", "绿灯", getTypeFromDeviceID(deviceID), DateTime.Now.ToString());
                            string sql = "UPDATE M_Device SET DeviceStatusID='053',UpdateTime=GETDATE() WHERE DeviceID='" + deviceID + "'";
                            sdh.ExecuteCommand(sql);
                            
                        }
                    };
                    break;
                case "RED":
                    {
                        if (paravalue == "True")
                        {
                            bool res = tDCS.ChangeDeviceStatus(deviceID, "051", "红灯", getTypeFromDeviceID(deviceID), DateTime.Now.ToString());
                            if (res == true)
                            {
                                MessageBox.Show("成功");
                            }
                            string sql = "UPDATE M_Device SET DeviceStatusID='051',UpdateTime=GETDATE() WHERE DeviceID='" + deviceID + "'";
                            sdh.ExecuteCommand(sql);
                            
                        }
                    };
                    break;
                //case "TURN":
                //    {
                //        if (paravalue == "1")
                //        {
                //            string sql = "UPDATE M_Device SET DeviceStatusID='023',UpdateTime=GETDATE() WHERE DeviceID='" + deviceID + "'";
                //            sdh.ExecuteCommand(sql);
                //            tDCS.ChangeDeviceStatus(deviceID, "023", "点亮", getTypeFromDeviceID(deviceID), DateTime.Now.ToString());
                //        }
                //    };
                //    break;
                case "YELLOW":
                    {
                        if (paravalue == "True")
                        {
                            tDCS.ChangeDeviceStatus(deviceID, "052", "黄灯", getTypeFromDeviceID(deviceID), DateTime.Now.ToString());
                            string sql = "UPDATE M_Device SET DeviceStatusID='052',UpdateTime=GETDATE() WHERE DeviceID='" + deviceID + "'";
                            sdh.ExecuteCommand(sql);
                            
                        }
                    };
                    break;
            }
        }
        #endregion

        private void button3_Click(object sender, EventArgs e)
        {
            string deviceID = "TS1";
            string type = getTypeFromDeviceID(deviceID);
            string date = DateTime.Now.ToString();
            tDCS.ChangeDeviceStatus(deviceID, "053", "绿灯", type, date);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {
            AlarmAnalysis aa = new AlarmAnalysis("火警  ::  15:54:40 08/18/1998  控制盘:01  卡:02  器件:0001 ");

            textBox1.Text = aa.AlarmType;
            textBox2.Text = aa.ControlPanel; ;
            textBox3.Text = aa.Card;
            textBox4.Text = aa.Device;
            dateTimePicker1.Text = aa.AlarmTime.ToString();

            textBox1.Text = COM.SP.PortName;
            
        }
    }
}
